'use strict';
//define(['common','angular','services'], function(common,angular) {
	angular.module('backcenter.roleSingle', [])

	.controller('roleSingleCtrl'
			,[ '$rootScope', '$scope', 'BizData'
			   , '$uibModalInstance','params', 'title'
			   , f]);//控制器结束
	function f($rootScope, $scope, BizData
			   , $uibModalInstance, params, title
			   ) {
		
		var Model = this;
		
		
		/**
		name:bizData#onNewCreateParam
		description: <b>[回调型事件]</b>业务新增数据创建新增参数事件，可以增加和修改用户自定义的参数
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"param" : {justep.Request.ActionParam} 新增参数对象,
			"defaultValues" : array - 新增行的默认值数组 ，数组的长度决定新增行数，数组中的对象是列的默认值
				defaultValues格式：
					[
						{列名:默认值,...},
						{列名:默认值,...},
						...
					]
		}
		</xmp>	
		@see justep.XData#newData
		@see justep.Request.ActionParam
		*/
		Model.dataRoleNewCreateParam = function(event) {
			event.param = {"billType": 'billType'};
		}
		
		/**
		name:bizData#onBeforeDelete
		description: <b>[回调型事件]</b>业务数据删除前
		@param {object} event 它的结构如下:<br/>
		{
			"source":组件的js对象
			,"cancel":可修改	，设置为true后中断当前delete动作
			,"deleteIDList":删除的行Id数组
		}
		*/
		Model.dataRoleBeforeDelete = function(event) {
		//	alert(event.deleteIDList[0]);
		}
		/**
		name:bizData#onAfterDelete
		description: <b>[回调型事件]</b>业务数据删除后
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"deleteIDList" : 删除的行Id数组
		}
		</xmp>	
		*/
		Model.dataRoleAfterDelete = function(event) {
		//	alert('删除之后！')
		}
		
		
		
		
		/**
		name:bizData#onIndexChanged
		description: <b>[回调型事件]</b>行记录变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"rowID" : 行Id,
			"rowIndex" : 行索引
		}
		</xmp>	
		*/
		Model.dataRoleIndexChanged = function(event) {
			
		}
		
		/**
		name:bizData#onDataChanged
		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
			"selfChanged" : 是否自身变化，false：从data触发的变化,
			"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
		}
		</xmp>	
		*/
		Model.dataRoleDataChanged = function(event) {
			
		}
		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"column" : 关系,
			"rowIndex" : 行索引,
			"value" : 新值,
			"originalValue" : 旧值
		}
		</xmp>	
		*/
		//输入值变化，监视input的输入，ng-change
		Model.dataRoleValueChanged = function(event) {
		
		}
		
		
		
		
		
		//定义页面级别的变量
		//var systemCode; //系统代码
		//var billType = 'GCDA';
		//var billTypeName = "";
		//var flowType = 0;
		//var autoCode; //是否自动编码
		//var revOpay; //入库出库
		//var direction;
		//var costingCode; //影响成本码
		//var origin;
		//var originName = "";
		//var billNo = "";
		
		/**
		name:bizData#onBeforeNew
		description: <b>[回调型事件]</b>业务数据新增前
		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
		*/
		Model.dataRoleBeforeNew = function(event) {
			
		};
		/**
		* name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
		* 
		* @param event
		*            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
		*/
		Model.dataRoleAfterNew = function(event){
			console.log('dataRoleAfterNew.event-->>',event);
			/*
			//创建行后赋值
			event.source.setValue('systemCode',systemCode,event.id);
			event.source.setValue('billType',billType,event.id);
			event.source.setValue('billTypeName',billTypeName,event.id);
			if (autoCode = "Y"){
				//产生自动流水号
				event.source.setValue('factoryNo',$scope.billNo,event.id);
			}
			*/
			//event.source.setValue('sCreatorFID', event.data.person.userId, event.id);	
		//	event.source.setValue('cUsercode', event.data.person.cUsercode, event.id);	
		//	event.source.setValue('cUsername', event.data.person.cUsername, event.id);	
		}
		
		
		Model.dataRoleAfterSave = function(event) {
			//保存成功
			$uibModalInstance.close(0);
		};
		
		
		//////////////////////对话框使用开始//////////////////////////////////////
		//模态窗口打开之后执行的事件
		$uibModalInstance.opened.then(function(){
			//接收调用对话框的参数
			$scope.dataRole.title = title;  
			//接收调用的参数
			Model.params = params; 
			console.log('Model.params-->',Model.params);
			 
			if(Model.params.recordState=='new'){
				$scope.dataRole.loadData('');
			     var options = {
				         index : 0,
				         defaultValues : [
				             {
		//		            	 state:'active'
		//		            	 ,status:'open'
		//		            	 ,audit:'active'
				            	 
				            	 sRoleKind:'fun'//'data'
				            	 ,sSequence:0
				            	 ,sValidState:1
				            	 
				            	 ,orgID: Model.params.id
				            	 ,sCreatorFID: ':getCurrentPersonID()'
				            	 ,version:0
				            }//,
		//			             {itemNo:'a2',itemDescribe:'b2'}
				         ]
				     };    
		
				$scope.dataRole.newData(options);
			}
			if(Model.params.recordState=='edit'){
				$scope.dataRole.id = Model.params.id;
			    $scope.dataRole.refreshData();
			}
		});  
		
		Model.ok = function () {  
			$scope.dataRole.saveData();
		};  
		
		Model.cancel = function () {  
			$uibModalInstance.dismiss('cancel');  
		};    
		//////////////////////对话框使用结束//////////////////////////////////////
		
		
		
		var options = {};
		options.container = Model;
		options.concept = 'SaOprole';
		options.name = 'dataRole';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '/backcenter/role/list/create';
		options.reader = '/backcenter/role/list/query';
		options.writer = '/backcenter/role/list/save';
		options.deleter = '/backcenter/role/list/delete';
		options.idColumnDefine = "sID";
		options.idColumnName = "sID";
//		options.defCols = {sID:{name:"sID",type:"String",label:""},sName:{name:"sName",type:"String",label:""},sCode:{name:"sCode",type:"String",label:""},sDescription:{name:"sDescription",type:"String",label:""},sRoleKind:{name:"sRoleKind",type:"String",label:""},sSequence:{name:"sSequence",type:"Integer",label:""},sValidState:{name:"sValidState",type:"Integer",label:""},sCatalog:{name:"sCatalog",type:"String",label:""},sParentRolesNames:{name:"sParentRolesNames",type:"String",label:""},orgID:{name:"orgID",type:"String",label:""},sCreatorFID:{name:"sCreatorFID",type:"String",label:""},version:{name:"version",type:"Integer",label:""}};
		options.isTree = false;
		options.parentRelation = 'sParent';
		options.nodeKindRelation = 'sNodeKind';
		options.nodeLevelRelation = 'sLevel';
		options.pathRelation = 'sFID';
		options.directDeleteMode = true;
		options.limit = 10;
		
		options.onIndexChanging = null;
		options.onIndexChanged = Model.dataRoleIndexChanged;
		options.onBeforeNew = Model.dataRoleBeforeNew;
		options.onAfterNew = Model.dataRoleAfterNew;
		options.onBeforeDelete = Model.dataRoleBeforeDelete;
		options.onAfterDelete = Model.dataRoleAfterDelete;
		options.onValueChanged = Model.dataRoleValueChanged;
		options.onDataChanged = Model.dataRoleDataChanged;
		options.onAfterSave = Model.dataRoleAfterSave;
		options.onNewCreateParam = Model.dataRoleNewCreateParam;
		
		$scope.dataRole = new BizData(options);
		
		//监视对象中的所有变动
		$scope.dataRole.deregRows = $scope.$watch('dataRole.rows',$scope.dataRole.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
		//////////////////////////////////////////////
		
		//接收调用的参数
		$scope.dataRole.params = $rootScope.$stateParams;  
		
		
		
		
		return Model;
		
		
	}	
	
//});
